<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>手写原型方法matchAll</title>
  </head>
  <body>
    <h1>houdunren.com</h1>
    <h2>hdcms.com</h2>
    <h1>baidu.com</h1>
  </body>
  <script>
    String.prototype.matchAll = function(reg) {
      let res = this.match(reg);
      if (res) {
        let str = this.replace(res[0], "^".repeat(res[0].length));
        let match = str.matchAll(reg) || [];
        return [res, ...match];
      }
    };

    const body = document.querySelector("body").innerHTML;
    let search = body.matchAll(/<(h[1-6])>([\s\S]+?)<\/\1>/i);
    console.log(search);
  </script>
</html>
